﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Kiiro.Common.Entities;
using Kiiro.Common.Utility;
using Kiiro.Model;
using Kiiro.SiteDefinition.Core;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class CustomFieldCollection : System.Web.UI.UserControl
    {
        public BasePage Base
        {
            get { return ( BasePage ) Page; }
        }

        public SPControlMode ControlMode
        {
            get { return ViewState [ "ControlMode" ] != null ? ( SPControlMode ) ViewState [ "ControlMode" ] : SPControlMode.Display; }
            set { ViewState [ "ControlMode" ] = value; }
        }

        public Type EntityType
        {
            get;
            set;
        }

        public string Title
        {
            get { return ViewState [ "Title" ] != null ? ( string ) ViewState [ "Title" ] : null; }
            set { ViewState [ "Title" ] = value; }
        }

        protected void Page_Load ( object sender, EventArgs e )
        {
            Visible = false;

            SPContext context = GetCustomFieldsContext ( );

            SPContentTypeId contentTypeID = new SPContentTypeId ( );
            if ( EntityType == typeof ( User ) )
            {
                contentTypeID = new SPContentTypeId ( Constants.SPContentTypeIds.User );
            }
            else if ( EntityType == typeof ( Project ) )
            {
                contentTypeID = new SPContentTypeId ( Constants.SPContentTypeIds.Project );
            }
            else if ( EntityType == typeof ( Work ) )
            {
                contentTypeID = new SPContentTypeId ( Constants.SPContentTypeIds.Work );
            }
            else if ( EntityType == typeof ( TeamMember ) )
            {
                contentTypeID = new SPContentTypeId ( Constants.SPContentTypeIds.TeamMember );
            }
            else if ( EntityType == typeof ( Discussion ) )
            {
                contentTypeID = new SPContentTypeId ( Constants.SPContentTypeIds.Discussion );
            }
            else if ( EntityType == typeof ( Comment ) )
            {
                contentTypeID = new SPContentTypeId ( Constants.SPContentTypeIds.Comment );
            }

            foreach ( SPContentType contentType in context.List.ContentTypes )
            {
                if ( contentType.Id.IsChildOf ( contentTypeID ) )
                {
                    foreach ( SPField field in contentType.Fields )
                    {
                        if ( field.Group == "Kiiro Custom Columns" )
                        {
                            if ( EntityType != typeof ( Work ) )
                                AddCustomFieldToPanel ( field, context );
                            else
                            {
                                // If a Work item's Custom Field is prefixed with task_ or issue_ only display it on the respective pages.
                                if ( field.Title.ToLower ( ).StartsWith ( "task_" ) )
                                {
                                    if ( Page.Request.Path.ToLower ( ).EndsWith ( "task.aspx" ) )
                                    {
                                        field.Title = field.Title.Remove ( 0, field.Title.IndexOf ( "_" ) + 1 );
                                        AddCustomFieldToPanel ( field, context );
                                    }
                                }
                                else if ( field.Title.ToLower ( ).StartsWith ( "issue_" ) )
                                {
                                    if ( Page.Request.Path.ToLower ( ).EndsWith ( "issue.aspx" ) )
                                    {
                                        field.Title = field.Title.Remove ( 0, field.Title.IndexOf ( "_" ) + 1 );
                                        AddCustomFieldToPanel ( field, context );
                                    }
                                }
                                else
                                    AddCustomFieldToPanel ( field, context );
                            }
                        }
                    }
                }
            }

            if ( Title != null )
            {
                litTitle.Text = Title;
                pnlTitle.Visible = true;
            }
        }

        private void AddCustomFieldToPanel ( SPField field, SPContext context )
        {
            CustomField customField = new CustomField ( );

            customField.FieldName = field.Title;
            customField.RequiredText = field.Required ? "<span class='validation-message'>*</span>" : default ( string );
            customField.HelpText = field.Description;

            if ( field.Type == SPFieldType.DateTime )
            {
                customField.EnableViewState = false;
            }

            BaseFieldControl baseFieldControl = field.FieldRenderingControl;
            customField.AddBaseFieldControl ( baseFieldControl );
            baseFieldControl.ControlMode = ControlMode;
            baseFieldControl.ItemContext = context;
            baseFieldControl.RenderContext = context;

            customField.SetVisible ( context );

            if ( !Visible && customField.Visible )
                Visible = true;

            pnlCustomFields.Controls.Add ( customField );
        }

        public Dictionary<string, string> HarvestCustomFieldValues ( )
        {
            Dictionary<string, string> fieldValues = new Dictionary<string, string> ( );

            foreach ( Control control in pnlCustomFields.Controls )
            {
                if ( control is CustomField )
                {
                    BaseFieldControl baseFieldControl = ( ( CustomField ) control ).BaseFieldControl;
                    if ( baseFieldControl.GetType ( ).Name != "RichTextField" )
                    {
                        if ( baseFieldControl.Value != null )
                        {
                            fieldValues.Add ( baseFieldControl.FieldName, baseFieldControl.Value.ToString ( ) );
                        }
                    }
                    else if ( baseFieldControl.GetType ( ).Name == "RichTextField" )
                    {
                        if ( ( ( HtmlInputControl ) baseFieldControl.Controls [ 0 ].Controls [ 1 ].Controls [ 3 ] ).Value != null )
                        {
                            string value = ( ( HtmlInputControl ) baseFieldControl.Controls [ 0 ].Controls [ 1 ].Controls [ 3 ] ).Value;
                            fieldValues.Add ( baseFieldControl.FieldName, value.ToString ( ) );
                        }
                    }
                }
            }

            return fieldValues;
        }

        private SPContext GetCustomFieldsContext ( )
        {
            SPContext context = CustomModel.GetCustomFieldsContext (
                EntityType,
                Base.ItemId,
                Context,
                Base.SiteCollectionId,
                Base.ServerRelativeUrl );

            context.FormContext.FormMode = ControlMode;

            return context;
        }
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/CustomFieldCollection.ascx.cs $
 * 
 * 13    30/09/09 4:08p Camerons
 * Update all links that point to UserPages, pass User.SPUserId as the
 * ItemId so that we can safely run GetOrCreateUser
 * 
 * 12    3/09/09 12:30p Shafia
 * 
 * 11    9/01/09 1:06p Stefanf
 * 
 * 10    1/09/09 11:32a Shafia
 * when a field is DateTime type, the EnableViewState property is set to
 * false to be able to save changed value. "customField.EnableViewState =
 * false;"
 * 
 * 9     31/08/09 4:31p Shafia
 * now we pull the custom fields from the content type instead of default
 * view
 * 
 * 8     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/